<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\widget\ajax;

use yii\helpers\Html;
use yii\helpers\Json;

/**
 * Ajax:模态框
 * ```php
 *  WidgetFacade::echo2Return(function () {
 *              \builder\widget\ajax\Modal::begin([
 *                      'id' => 'eeeeeeeeeeeeeeeeeeee', //如果用于页面遍历中,则必须指定不同`id`!
 *                      'title' => 'ModalB4 title',
 *                      'bodyOptions' => [
 *                          'class' => 'text-center',
 *                      ],
 *                      'toggleButton' => [
 *                          'label' => 'ModalB4 btn',
 *                          'class' => 'layui-btn layui-btn-sm layui-btn-primary',
 *                      ],
 *              ]);
 *              echo implode("\n", array_fill(0, 100, 'ModalB4 content'));
 *              \builder\widget\ajax\Modal::end();
 * });
 *
 * ```
 * @author cleverstone
 * @since ym2.0
 */
class Modal extends \yii\bootstrap4\Modal
{
    public static $autoIdPrefix = 'B4-Modal';

    /**
     * Renders the widget.
     */
    public function run()
    {
        echo "\n" . $this->renderBodyEnd();
        echo "\n" . $this->renderFooter();
        echo "\n" . Html::endTag('div'); // modal-content
        echo "\n" . Html::endTag('div'); // modal-dialog
        echo "\n" . Html::endTag('div');
    }

    /**
     * Initializes the widget options.
     * This method sets the default values for various options.
     */
    protected function initOptions()
    {
        parent::initOptions();
        $this->options['ym2-bootstrap-plugin'] = 'modal';
        $this->options['plugin-config'] = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
    }
}